<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/header.php");
?>

<section>
<hgroup>
<h1>Wachtwoord veranderen</h1>
</hgroup>

<script>
function passwordStrength(password)
{
	var desc = new Array();
	desc[0] = "Zeer zwak";
	desc[1] = "Zwak";
	desc[2] = "Beter";
	desc[3] = "Medium";
	desc[4] = "Sterk";
	desc[5] = "Sterkst";

	var score   = 0;

	//if password bigger than 6 give 1 point
	if (password.length > 6) score++;

	//if password has both lower and uppercase characters give 1 point	
	if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;

	//if password has at least one number give 1 point
	if (password.match(/\d+/)) score++;

	//if password has at least one special caracther give 1 point
	if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) )	score++;

	//if password bigger than 12 give another 1 point
	if (password.length > 12) score++;

	document.getElementById("passwordDescription").innerHTML = desc[score];
	document.getElementById("passwordStrength").className = "strength" + score;
}
</script>


<?php if(!isset($_POST['Submit'])) { ?>
<form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

  <table style="margin-left: 25px">
    <tr>
      <td><p>Oud wachtwoord:</p></td>
      <td><input name="wachtwoordoud" type="password" size="35" /></td>
    </tr>
    <tr>
      <td><p>Nieuw wachtwoord:</p></td>
      <td><input name="wachtwoord" type="password" size="35" onkeyup="passwordStrength(this.value)" /></td>
    </tr>
    <tr>
      <td><p>Nieuw wachtwoord herhalen:</p></td>
      <td><input name="wachtwoord2" type="password" size="35"></td>
    </tr>
    <tr>
        <td>Wachtwoord sterkte:</td>
	<td>
            <div id="passwordDescription">Geen wachtwoord ingegeven</div>
            <div id="passwordStrength" class="strength0"></div>
        </td>
    </tr>
    <tr>
      <td colspan="2"><input type="submit" name="Submit" value="Veranderen"></td>
    </tr>
  </table>
</form>
<?php } else {
    $error = array();
    $checkp = mysql_result(mysql_query("SELECT COUNT(*) FROM user WHERE UserName='".$_SESSION['username']."' AND PassWord='".sha1(strtoupper($_SESSION['username']).$_POST['wachtwoordoud'])."'"),0);

    if($_POST['wachtwoord'] != "" && $checkp == 0) {
        $error[] = '<p><span style="color:red">Het oude wachtwoord is niet correct.</span></p>';
    }

    if($_POST['wachtwoord'] == "" || $_POST['wachtwoord'] != $_POST['wachtwoord2']) {
        $error[] = '<p><span style="color:red">Er is geen wachtwoord ingevuld of de 2 wachtwoorden komen niet overeen.</span></p>';
    }

    $errors = sizeof($error); // aantal errors tellen
    if($errors != 0) { // Er is minstens 1 error
        echo '<p><span style="color:red">Uw wachtwoord kon niet verandert worden omwille van de volgende reden(en):</span></p>';
        for($i = 0; $i < $errors; $i++) {
            echo $error[$i];
    }

    } else {	
        mysql_query("UPDATE user SET PassWord = '".sha1(strtoupper($_SESSION['username']).$_POST['wachtwoord'])."' WHERE Id ='".$_SESSION['userid']."'");
        echo "<p>Je wachtwoord is veranderd, de volgende keer kan je het nieuwe wachtwoord gebruiken om in te loggen.</p>";
    }
}
?>


</section>

<?php
    include($_SERVER['DOCUMENT_ROOT']."/ObstacleWebApp/view/footer.php");
?>
